<template>
	<view id="box">
		<view class="revice_address_row">
			<!-- 当收货地址 不存在 按钮显示  对象 空对象 bool类型也是true  -->
			<view class="address_btn" v-if="!address.userName">
				<button @click="handleChooseAddress" type="primary" plain>获取收货地址</button>
			</view>
			<!-- 当收货地址 存在 详细信息就显示 -->
			<view v-else class="user_info_row">
				<view class="user_info">
					<view>{{address.userName}}</view>
					<view>{{address.provinceName}}{{address.cityName}}{{address.countyName}}{{address.detailInfo}}</view>
				</view>
				<view class="user_phone">{{address.telNumber}}</view>
			</view>
		</view>

		<view class="cart_content">
			<view class="cart_title">购物车</view>
			<view class="cart_main">
				<!-- 当cart数组 长度不为0 显示 商品信息 -->
				<block v-if="cart.length!==0">
					<view class="cart_item" v-for="item in cart" :key="item.id">
						<!-- 复选框 -->
						<view class="cart_chk_wrap">
							<checkbox-group :data-id="item.id" @change="handleItemChange">
								<checkbox :checked="item.checked"></checkbox>
							</checkbox-group>
						</view>
						<!-- 商品图片 -->
						<navigator class="cart_img_wrap" :url="`/pages/goodDetail/goodDetail?goods_id=${item.id}`">
							<image mode="widthFix" :src="item.smallLogo"></image>
						</navigator>
						<!-- 商品信息 -->
						<view class="cart_info_wrap">
							<view class="goods_name">{{item.name}}</view>
							<view class="goods_price_wrap">
								<view class="goods_price">￥{{item.price}}</view>
								<view class="cart_num_tool">
									<view @click="handleItemNumEdit" :data-id="item.id" :data-operation="-1"
										class="num_edit">-</view>
									<view class="goods_num">{{item.num}}</view>
									<view @click="handleItemNumEdit" :data-id="item.id" :data-operation="1"
										class="num_edit">+</view>
								</view>
							</view>
						</view>
					</view>
				</block>
				<block v-else>
					<image mode="widthFix"
						src="http://hbimg.b0.upaiyun.com/e1b1467beea0a9c7d6a56b32bac6d7e5dcd914f7c3e6-YTwUd6_fw658">
					</image>
				</block>
			</view>
		</view>
		
		<!-- 底部工具栏 -->
		<view class="footer_tool">
		  <!-- 全选 -->
		  <view class="all_chk_wrap">
		    <checkbox-group @change="handleItemAllCheck">
		      <checkbox :checked="allChecked">全选</checkbox>
		    </checkbox-group>
		  </view>
		  <!-- 总价格 -->
		  <view class="total_price_wrap">
		    <view class="total_price">
		      合计: <text class="total_price_text">￥{{totalPrice}}</text>
		    </view>
		    <view>包含运费</view>
		  </view>
		  <!-- 结算 -->
		  <view class="order_pay_wrap" @click="handlePay" >
		    结算({{totalNum}})
		  </view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				address: {},
				cart: [],
				allChecked: false,
				totalPrice: 0,
				totalNum: 0
			};
		},
		onShow() {
			this.address = uni.getStorageSync('address');
			this.cart = uni.getStorageSync('cart') || []
			this.setCart()
		},
		methods: {
			setCart() {

				this.allChecked = true;
				this.totalPrice=0
				this.totalNum=0
				this.cart.forEach(v => {
					if (v.checked) {
						this.totalPrice += v.num * v.price;
						this.totalNum += v.num;
					} else {
						this.allChecked = false
					}
				})
				this.allChecked = (this.cart.length != 0) ? this.allChecked : false
			
				uni.setStorageSync("cart", this.cart)
			},
			handleChooseAddress() {
				uni.chooseAddress({
					success: (res) => {
						console.log(this)
						this.address = res
						this.address.all = res.provinceName + res.cityName + res.countyName + res.detailInfo;
						uni.setStorageSync("address", res)
					},
					fail(err) {
						console.log(err)
					}
				})
			},
			//商品的选中
			handleItemChange(e) {
				const goods_id = e.currentTarget.dataset.id;
				let index = this.cart.findIndex(v => v.id == goods_id)
				this.cart[index].checked = !this.cart[index].checked
				this.setCart()
			},
			//商品全选
			handleItemAllCheck() {
				this.allChecked = !this.allChecked
				console.log(this.allChecked)
				this.cart.forEach(v => v.checked = this.allChecked)
				this.setCart()
			},
			//商品数量编辑
			handleItemNumEdit(e) {
				const {
					operation,
					id
				} = e.currentTarget.dataset;
				const index = this.cart.findIndex(v => v.id === id)
				if (this.cart[index].num === 1 && operation === -1) {
					uni.showModal({
						content: '您是否要删除',
						success:()=> {
							
							this.cart.splice(index, 1)
							this.setCart()
						}
					})
				} else {
					this.cart[index].num += operation
					this.setCart()
				}
			},
			//点击结算
			handlePay() {
				if (!this.address.userName) {
					uni.showToast({
						title: '您还没有选择收货地址',
					})
					return;
				}
				if (this.totalNum === 0) {
					uni.showToast({
						title: "您还没有选购商品"
					});
					return;
				}
				wx.navigateTo({
					url: '/pages/pay/pay'
				});

			},
			// 设置购物车状态同时 重新计算 底部工具栏的数据 全选 总价格 购买的数量
		
		}



	}
</script>

<style lang="scss" scoped>
#box{
	font-size: 28rpx;
  
  padding-bottom: 120rpx;
  
}
.revice_address_row{
  .address_btn{
    padding: 20rpx;
    button{
      width: 60%;
    }
  }
  .user_info_row{
    display: flex;
    padding: 20rpx;
    .user_info{
			font-size: 28rpx;
      flex: 5;
    }
    .user_phone{
			font-size: 28rpx;
      flex: 3;
      text-align: right;
    }
  }
}

.cart_content {
  .cart_title {
    padding: 20rpx;
    font-size: 36rpx;
    color: $theme-color;
    border-top: 1rpx solid currentColor;
    border-bottom: 1rpx solid currentColor;
  }

  .cart_main {
    .cart_item {
      display: flex;
      padding: 10rpx;
      border-bottom: 1rpx solid #ccc;

      .cart_chk_wrap {
        flex: 1;
        display: flex;
        justify-content: center;
        align-items: center;
        checkbox-group {
          checkbox {

          }
        }
      }

      .cart_img_wrap {
        flex: 2;
        display: flex;
        justify-content: center;
        align-items: center;
        image {
          width: 80%;
        }
      }

      .cart_info_wrap {
        flex: 4;
        display: flex;
        flex-direction: column;
        justify-content: space-around;
        .goods_name {
          display: -webkit-box;
          overflow: hidden;
          -webkit-box-orient: vertical;
          -webkit-line-clamp:2;
          color: #666;
        }

        .goods_price_wrap {
          display: flex;
          justify-content: space-between;
          .goods_price {
            color: $theme-color;
            font-size: 34rpx;
          }

          .cart_num_tool {
            display: flex;
            .num_edit {
              width: 55rpx;
              height: 55rpx;
              display: flex;
              justify-content: center;
              align-items: center;
              border: 1rpx solid #ccc;
            }

            .goods_num {
              width: 55rpx;
              height: 55rpx;
              display: flex;
              justify-content: center;
              align-items: center;
            }
          }
        }
      }
    }
  }
}

.footer_tool {
  position: fixed;
	/* #ifdef H5 */
  bottom: 100rpx;
	/* #endif */
	/* #ifdef MP-WEIXIN */
	bottom: 0;
	/* #endif */
  left: 0;
  width: 100%;
  height: 90rpx;
  background-color: #fff;
  display: flex;
  border-top: 1rpx solid #ccc;
  .all_chk_wrap {
    flex: 2;
    display: flex;
    justify-content: center;
    align-items: center;
  }

  .total_price_wrap {
    flex: 5;
    padding-right: 15rpx;
    text-align: right;
    .total_price {
      .total_price_text {
        color: $theme-color;
        font-size: 34rpx;
        font-weight: 600;
      }
    }

  }

  .order_pay_wrap {
    flex: 3;
    background-color: $theme-color;
    color: #fff;
    font-size: 32rpx;
    font-weight: 600;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
</style>
